﻿using FileManager.Util;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace FileManager.Model
{
    public record MetaData
    {
        [XmlElement(ElementName = "FileName")]
        public string FileName => Fi.Name;
        [XmlElement(ElementName = "Path")]
        public required string Path { get; init; }
        private FileInfo fi;
        [XmlIgnore]
        private FileInfo Fi
        {
            get
            {
                if (fi == null)
                    fi = new FileInfo(Path);
                return fi;
            }
            set => fi = value;
        }

        [XmlIgnore]
        public string Extension => Fi.Extension;

        [XmlIgnore]
        public string ParentPath => Fi.DirectoryName ?? Fi.FullName;
        [XmlElement(ElementName = "CreateTime")]
        public DateTime CreateTime => createTime;

        [XmlElement(ElementName = "LastWriteTime")]
        public DateTime LastWriteTime => lastModifiedTime;

        [XmlElement(ElementName = "Author")]
        public string Author => creator;

        [XmlElement(ElementName = "LastWriter")]
        public string LastWriter => lastWriter;

        [XmlElement(ElementName = "Tag")]
        public List<string> Tag { get; set; } = new List<string>();
        [XmlElement(ElementName = "Description")]
        public string? Description { get; set; }
        
        [XmlElement(ElementName = "Hash")]
        public required string Hash { get; init; }
        [XmlAttribute(AttributeName = "path")]
        public string InitialPath { get; set; } // 添加一个专门用来在构造函数中初始化的属性

        private DateTime lastModifiedTime;
        private string creator;
        private string lastWriter;
        private DateTime createTime;
        [OnDeserialized]
        internal void OnDeserializedMethod(StreamingContext context)
        {
            // 反序列化完成后，根据已填充的Path属性值初始化fi对象
            fi = new FileInfo(Path);
            IniOfficeInfo();
        }

        public static MetaData FromFile(string path)
        {
            return new MetaData(path);
        }
        
        [SetsRequiredMembers]
        public MetaData(string path)
        {
            InitialPath = path;
            Path = path;
            Hash = HashHelper.GetFileHash256(path);
            fi = new FileInfo(Path);
            IniOfficeInfo();
        }
        public MetaData ReLoad(string path)
        {
            return new MetaData(path);
        }
        public void IniOfficeInfo()
        {
            this.lastModifiedTime = fi.LastWriteTime;
            this.createTime = fi.CreationTime;

        }
        public void SetOfficeInfo(string creator,string modified,DateTime createTime,DateTime lastModifiedTime)
        {

            this.creator = creator;
            this.lastWriter = modified;
            this.createTime = createTime;
            this.lastModifiedTime = lastModifiedTime;
        }
        public MetaData()
        {
        }
    }
}
